package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 文件上传相关接口
 */
@RestController
@Slf4j
@Api(tags = "文件上传相关接口")
public class CommonController {
    @Autowired
    private AliOssUtil aliOssUtil;

    @PostMapping("/admin/common/upload")
    @ApiOperation("文件上传")
    public Result upload(MultipartFile file){
        String filename = file.getOriginalFilename();
        String str = filename.substring(filename.lastIndexOf("."));

        String extend = UUID.randomUUID() + str;
        try {
            String url = aliOssUtil.upload(file.getBytes(), extend);
            return Result.success(url);
        } catch (IOException e) {
            return Result.error("文件上传失败");
        }
    }
}
